<template>
  <el-drawer size="80%" v-model="dialogVisible" :title="dialogTitle" width="500">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="80px"
    >
    <el-form-item label="消息分类" prop="category">
        <el-radio-group v-model="formData.category">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.AGENT_WALL_MESSAGE_CATEGORY)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="消息内容" prop="content">
        <el-input
          v-model="formData.content"
          type="textarea"
          :rows="20"
          placeholder="请输入消息内容"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>
      
       <el-form-item label="作者" prop="version">
        <el-input v-model="formData.creatorName" placeholder="匿名" />
      </el-form-item>
      <!-- <el-form-item label="版本" prop="version">
        <el-input v-model="formData.version" placeholder="匿名" />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import { ref, reactive, defineEmits, defineExpose } from 'vue'
import { ElMessage } from 'element-plus'
import { WallMessageApi, type WallMessageVO } from '@/api/agent/manage/wallMessage'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

defineOptions({ name: 'WallMessageForm' })

const dialogVisible = ref(false)
const dialogTitle = ref('')
const formLoading = ref(false)
const formRef = ref()
const formData = ref<WallMessageVO>({
  id: undefined,
  content: '',
  category: "1",
  creatorName:'',
  version: 'v1.0.0',
  likes: 0
})
const formRules = reactive({
  content: [{ required: true, message: '消息内容不能为空', trigger: 'blur' }],
  category: [{ required: true, message: '消息分类不能为空', trigger: 'change' }],
  version: [{ required: true, message: '版本号不能为空', trigger: 'blur' }]
})

const emit = defineEmits(['success'])

const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = type === 'create' ? '发布消息' : '编辑消息'
  resetForm()
  if (id) {
    formLoading.value = true
    try {
      formData.value = await WallMessageApi.getWallMessage(id)
    } finally {
      formLoading.value = false
    }
  }
}

defineExpose({ open })

const submitForm = async () => {
  const valid = await formRef.value.validate()
  if (!valid) return

  formLoading.value = true
  try {
    const data = formData.value as WallMessageVO
    if (dialogTitle.value === '发布消息') {
      await WallMessageApi.createWallMessage(data)
      ElMessage.success('发布成功')
    } else {
      await WallMessageApi.updateWallMessage(data)
      ElMessage.success('修改成功')
    }
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const resetForm = () => {
  formData.value = {
    id: undefined,
    content: '',
    category: 1,
    version: 'v1.0.0',
    likes: 0
  }
  formRef.value?.resetFields()
}
</script>